package com.emotion.sshticket.interceptors;

import java.util.Enumeration;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.emotion.sshticket.vo.AdminUserVO;

public class CommonInterceptor extends HandlerInterceptorAdapter {

	@Resource(name="propertySource")
	private Properties propertySource;

	@Override
	public boolean preHandle(
			HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {

		if (propertySource.get("system.debug").equals("Y")) {
			String indent = "	";
			String lineSeparator = System.getProperty("line.separator");

			StringBuffer sb = new StringBuffer();
			sb.append(lineSeparator + "== Headers ==" + lineSeparator);
			@SuppressWarnings("unchecked")
			Enumeration<String> headerEnum = request.getHeaderNames();
			while (headerEnum.hasMoreElements()) {
				String key = (String) headerEnum.nextElement();
				sb.append(indent + key + " : " + request.getHeader(key) + lineSeparator);
			}
			sb.append(indent + "ip : " + request.getRemoteAddr() + lineSeparator);
			sb.append(indent + "url : " + request.getRequestURI() + lineSeparator);
			sb.append(indent + "method : " + request.getMethod() + lineSeparator);
			sb.append(indent + "locale : " + request.getLocale() + lineSeparator);
			System.out.println(sb.toString());

			sb = new StringBuffer();
			sb.append(lineSeparator + "== Parameters ==" + lineSeparator);
			@SuppressWarnings("unchecked")
			Enumeration<String> paramEnum = request.getParameterNames();
			while (paramEnum.hasMoreElements()) {
				String key = (String) paramEnum.nextElement();
				String value = request.getParameter(key);
				sb.append(indent + key + " : " + new String(value.getBytes("8859_1"),"UTF-8") + lineSeparator);
			}
			System.out.println(sb.toString());
		}

		return super.preHandle(request, response, handler);
	}


}
